<?php
class Promotionproject extends CS_Controller
{
    private $belong = array(
        0 => '网站大首页',
        1 => '景区专题',
        2 => '酒店专题',
        3 => '线路专题',
        4 => '商品专题',
        5 => '手机wap端首页超值购',
        6 => '手机wap端周边游热门目的地',
        7 => '手机wap端首页三促销',
        8 => '手机wap端首页出游季促销',
        9 => '手机wap端首页出游季左侧产品',
    );
    
    private $version = array(
        1 => '电脑PC版',
        2 => '手机wap版',
        3 => '手机Android版',
        4 => '手机IOS版'
    );
    
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('promotion_project_model', 'romotion_project');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('promotionproject/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('promotionproject/grid');
        $config['total_rows'] = $this->romotion_project->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->romotion_project->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['page_num'] = $page_num;
        $data['belong'] = $this->belong;
        $data['version'] = $this->version;
        $this->load->view('promotionproject/grid', $data);
    }

    public function add()
    {
        $data['belong'] = $this->belong;
        $data['version'] = $this->version;
        $this->load->view('promotionproject/add', $data);
    }

    public function addPost()
    {
        $error = $this->validate();
        if ( empty($_FILES['images']['name']) ) {
            $error[] = '图片添加不能为空';
        }
        if ( !empty($error) ) {
            $this->error('promotionproject/add', '', $error);
        }
        $imageData = $this->dealWithImages('images', '', 'promotion');
        if ( $imageData == false ) {
            $this->error('promotionproject/add', '', '图片上传失败！');
        }

        $ifResize = $this->dealWithImagesResize($imageData, 200, 130);
        if ( $ifResize == false ) {
            $this->error('promotionproject/add', '', '缩略图生成失败！');
        }

        $this->db->trans_start();
        $resultId = $this->romotion_project->insertPromotionProject($this->input->post(), $imageData);
        $this->db->trans_complete();

        if ( $this->db->trans_status() === TRUE ) {
            $this->success('promotionproject/grid', '', '保存成功！');
        } else {
            $this->error('promotionproject/add', '', '保存失败！');
        }
    }

    public function edit($id)
    {
        $result = $this->romotion_project->findById($id);
        if ( $result->num_rows() <= 0 ) {
            $this->redirect('promotionproject/grid');
        }
        $data['promotion'] = $result->row();
        $data['belong'] = $this->belong;
        $data['version'] = array(1 => 'PC版', 2 => '手机版');
        $this->load->view('promotionproject/edit', $data);
    }

    public function editPost()
    {
        $id = $this->input->post('id');
        $error = $this->validate();

        if ( !empty($error) ) {
            $this->error('promotionproject/edit', $id, $error);
        }
        $imageData = '';
        if ( !empty($_FILES['images']['name']) ) {
            $imageData = $this->dealWithImages('images', $this->input->post('oldfilename'), 'promotion');
            if ( $imageData == false ) {
                $this->error('promotionproject/edit', $id, '图片上传失败！');
            }

            $ifResize = $this->dealWithImagesResize($imageData, 200, 130);
            if ( $ifResize == false ) {
                $this->error('promotionproject/edit', $id, '缩略图生成失败！');
            }
        }
        $this->db->trans_start();
        $resultId = $this->romotion_project->updatePromotionProject($this->input->post(), $imageData);
        $this->db->trans_complete();

        if ( $this->db->trans_status() === TRUE ) {
            $this->success('promotionproject/grid', '', '保存成功！');
        } else {
            $this->error('promotionproject/edit', $id, '保存失败！');
        }
    }

    public function validate()
    {
        $error = array();
        if ( $this->validateParam($this->input->post('name')) ) {
            $error[] = '专题名称不能为空';
        }
        if ( $this->validateParam($this->input->post('url')) ) {
            $error[] = 'URL地址不能为空';
        }
        if ( $this->validateParam($this->input->post('version')) ) {
            $error[] = '专题来源不能为空';
        }

        return $error;
    }

    public function del($id)
    {
        $status = $this->romotion_project->delete($id);
        if ( $status ) {
            $this->success('promotionproject/grid', '', '删除成功！');
        } else {
            $this->error('promotionproject/grid', '', '删除失败！');
        }
    }
}